﻿using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Media;

using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Common.Services;

using SBPweb.MediaCenter.Tools.WPF.Extenders;

using SBPweb.MediaCenter.Modules.Player.WPF.Controls;

namespace SBPweb.MediaCenter.Modules.Player.WPF
{

    public class Module : Player.Module
    {

        int i = 0;

        #region Constructor logic

        public Module(IModuleHost parent)
            : base(parent)
        {
            Name = "Player";
        }

        #endregion

        #region Property accessors

        public new Controllers.PlayerService Player
        {
            get
            {
                return base.Player as Controllers.PlayerService;
            }
            protected set
            {
                base.Player = value;
            }
        }

        #endregion

        #region Methods

        #region Initialization logic

        public override void InstantiateServices()
        {
            base.InstantiateServices();
            Player = new Controllers.PlayerService(this);
        }

        public override void Initialize()
        {
            base.Initialize();

            if (HostType != ModuleHostType.WindowsPresentationFoundation)
            {
                //TODO: log something
                return;
            }
        }

        public override void InitializeMenus()
        {
            //Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SBPweb.MediaCenter.Modules.Player.WPF.Resources.MenuIcon.3DS");
            //MenuItem mItem = new MenuItem(stream);
            //mItem.BackgroundColor = Color.FromRgb(0, 60, 168);
            //Host.Menu.Add(mItem);
            MainMenuItem mmItem = new MainMenuItem();
            mmItem.BackgroundColor = Color.FromRgb(0, 60, 168);
            mmItem.ParentModule = this;
            pHost.Menu.Add(mmItem);
        }

        public override void InitializeSmartparts()
        {
            PlayerControl pc = new PlayerControl(this);
            pHost.Smartparts.Add(pc);
            Player.PlayerViewControl = pc;
        }

        #endregion

        //private void GenerateControls()
        //{
        //    Window window = pHost as Window;
        //    for (; i < 10; ++i)
        //    {
        //        Thread.Sleep(200);
        //        //CreateControl();
        //        window.Dispatcher.Invoke(new InvokeDelegate(CreateControl));
        //    }
        //}

        //private void CreateControl()
        //{
        //    MainMenuItem mmItem = new MainMenuItem();
        //    mmItem.Text = string.Format("{0} player", i);
        //    Random rnd = new Random();
        //    mmItem.BackgroundColor = Color.FromRgb((byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255));
        //    pHost.Menu.Add(mmItem);
        //    //PlayerControl pWPFPlayerControl = new PlayerControl(this);
        //    //pHost.Smartparts.Add(pWPFPlayerControl);
        //}

        //protected override void OnLayoutServiceInitialized(object sender, EventArgs e)
        //{
        //    base.OnLayoutServiceInitialized(sender, e);
        //    Thread th = new Thread(new ThreadStart(GenerateControls));
        //    th.SetApartmentState(ApartmentState.STA);
        //    th.Start();
        //}

        public override void LoadPersistency()
        {
            throw new NotImplementedException();
        }

        public override void SavePersistency()
        {
            throw new NotImplementedException();
        }

        public override void Terminate()
        {
            //throw new NotImplementedException();
        }

        #endregion

    }

}